//
//  CNADRotatorView.m
//  BeeQuick_19
//
//  Created by tanky on 2016/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNADRotatorView.h"
#import "CNADRotatorModel.h"
#import "CNHomeTableViewController.h"

@interface CNADRotatorView ()

@end

@implementation CNADRotatorView

#pragma mark View建立的时候 设置frame 并加载网络数据
- (instancetype)init{
    [self loadADData];
    return [CNADRotatorView cycleScrollViewWithFrame:self.bounds delegate:nil placeholderImage:[UIImage imageNamed:@"v2_placeholder_half_size"]];
}

- (instancetype)initWithFrame:(CGRect)frame{
    [self loadADData];
    return [super initWithFrame:frame];
}

#pragma mark 有模型数组设置的时候 加载轮播器
- (void)setRotatorData:(NSArray *)rotatorData{
    if (_rotatorData != rotatorData) {
        _rotatorData = rotatorData;
        [self setupADRotator];
    }
}

#pragma mark 加载网络数据 并赋值
- (void)loadADData{
    CNHTTPSessionManager *manager = [CNHTTPSessionManager sharedManager];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"1" forKey:@"call"];
    
    [manager request:CNHttpMethodPost componentUrlString:@"focus.json.php" parameters:param completion:^(id response, NSError *error) {
        NSArray *arr = [NSArray yy_modelArrayWithClass:[CNADRotatorModel class] json:response[@"data"][@"focus"]];
        self.rotatorData = arr.copy;
    }];

}

#pragma mark 加载轮播器
- (void)setupADRotator{
    NSMutableArray *mArr = [NSMutableArray array];
    for (CNADRotatorModel *model in self.rotatorData) {
        [mArr addObject:model.img];
    }
    //轮播器图片URL数据
    NSArray *imagesURLStrings = [NSArray arrayWithArray:mArr];
    self.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    self.imageURLStringsGroup = imagesURLStrings;
    CNHomeTableViewController *homeVC = [self getCurrentViewController];
    self.delegate = homeVC;
}

/** 获取当前View的控制器对象 */
- (CNHomeTableViewController *)getCurrentViewController{
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[CNHomeTableViewController class]]) {
            return (CNHomeTableViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}


@end
